//
//  UIView+BKOrderMenu.m
//  bestkeep
//
//  Created by yons on 16/11/30.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "UIView+BKOrderMenu.h"

@implementation UIView (BKOrderMenu)

- (void)show
{
    __block CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -1.0/900;
    UIView *rootView = [self rootView];
    UIView *BGView = [self BGView];
    [rootView addSubview:BGView];
    
    CGRect startFrame = CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    CGRect endFrame = self.frame;
    
    transform = CATransform3DScale(transform, 0.91, 0.91, 1);
    transform = CATransform3DRotate(transform, M_PI / 180.0 * 6, 1, 0, 0);
    
    self.frame = startFrame;
    self.layer.anchorPointZ = -100;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    
    [UIView animateWithDuration:0.4 animations:^{
        
        rootView.layer.transform = transform;
        self.frame = endFrame;
        BGView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.4];
        
    } completion:^(BOOL finished) {
        transform = CATransform3DRotate(transform, - M_PI / 180.0 * 6, 1, 0, 0);
        [UIView animateWithDuration:0.4 animations:^{
            rootView.layer.transform = transform;
        } completion:^(BOOL finished) {
            
        }];
    }];
    
}

- (void)dismiss
{
    __block CATransform3D transform = CATransform3DIdentity;
    transform.m34 = -1.0/900;
    UIView *rootView = [self rootView];
    UIView *BGView = [self BGView];
    
    CGRect endFrame = CGRectMake(0, CGRectGetHeight([UIScreen mainScreen].bounds), CGRectGetWidth(self.frame), CGRectGetHeight(self.frame));
    
    
    transform = CATransform3DScale(transform, 1, 1, 1);
    
    [UIView animateWithDuration:0.4 animations:^{
        self.frame = endFrame;
        rootView.layer.transform = transform;
        BGView.backgroundColor = [UIColor clearColor];
        
    } completion:^(BOOL finished) {
        rootView.layer.transform = CATransform3DIdentity;
        [UIApplication sharedApplication].keyWindow.backgroundColor = [UIColor whiteColor];
        [self removeFromSuperview];
        [BGView removeFromSuperview];
        
    }];
}

- (UIView *)rootView
{
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    keyWindow.backgroundColor = [UIColor blackColor];
    UIView *rootView = keyWindow.rootViewController.view;
    return rootView;
}

- (UIView *)BGView
{
    UIView *BGView = [[self rootView] viewWithTag:1949];
    if (BGView) {
        return BGView;
    }
    
    BGView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    BGView.tag = 1949;
    BGView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.1];;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
    [BGView addGestureRecognizer:tap];
    [[self rootView] addSubview:BGView];
    return BGView;
}
@end
